package com.woniu.order.service.impl;

import cn.hutool.core.util.IdUtil;
import cn.hutool.core.util.ObjectUtil;
import com.alibaba.spring.util.ObjectUtils;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.woniu.common.utils.ResponseData;
import com.woniu.order.dto.InvoiceDto;
import com.woniu.order.pojo.Invoice;
import com.woniu.order.service.InvoiceService;
import com.woniu.order.mapper.InvoiceMapper;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;

/**
* @author Administrator
* @description 针对表【invoice】的数据库操作Service实现
* @createDate 2024-08-12 15:20:10
*/
@Service
public class InvoiceServiceImpl extends ServiceImpl<InvoiceMapper, Invoice>
    implements InvoiceService{
    @Override
    public ResponseData<?> addInvoice(InvoiceDto invoice) {
        Invoice invoice1 = new Invoice();
        invoice1.setId(String.valueOf(IdUtil.getSnowflake(1,1).nextId()));
        invoice1.setBuyerId(invoice.getOrderDetails().get(0).getUserId());
        invoice1.setAddressId(invoice.getAddress().getId());
        invoice1.setInvoiceTotalPrice(new BigDecimal(invoice.getTotalPrice()));
        invoice1.setOrderId(invoice.getOrderDetails().get(0).getOrderId());
        Invoice one = lambdaQuery().eq(Invoice::getOrderId, invoice1.getOrderId())
                .one();
        if(ObjectUtil.isEmpty(one)){
            boolean b = save(invoice1);
            return b ? new ResponseData<>().success() : new ResponseData<>().fail();
        }else {
            return new ResponseData<>().success();
        }
    }
}




